module.exports = {
	getUserByOpenId: getUserByOpenId,
	getMemberByAccount: getMemberByAccount,
	bind: bind,
	getLastOrder: getLastOrder,
	getMemberById: getMemberById,
	getInventory: getInventory,
	toStoreTime: toStoreTime,
	getBindList: getBindList,
	deleteBind: deleteBind,
	updatePassword: updatePassword,
	saveMember: saveMember
};
var uuid = require('../common/utils/id-helper');
var db = require('../common/database/db');

function saveMember(member, cb) {
	db.queryWithParam("insert into member (id, birth, createTime, joinDate, memberNumber,mobile,name,sex,creator_id,memberType_id,merchant_id,store_id,mgmtDomain,searchCode) " +
	"values (?, ?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?)", [member.id, member.birth, member.createTime, member.joinDate, member.memberNumber,
		member.mobile, member.name, member.sex, member.creator_id, member.membertype_id, member.merchant_id, member.storeId, member.mgmtDomain, member.searchCode], cb);
}

function updatePassword(pwd, memberId, cb) {
	db.queryWithParam('update member set password=? where id=?', [pwd, memberId], cb);
}

function toStoreTime(memberId, storeId, cb) {
	db.queryWithParam("SELECT COUNT(id) AS total FROM orders WHERE memberId = ? and storeId=? and orderType = 'sales'", [memberId, storeId], function (err, data) {
		if (err) return cb(err);
		cb(null, data[0]);
	});
}

function deleteBind(bindId, cb) {
	db.queryWithParam("delete from member_sns_bind where id=?", [bindId], cb)
}
function getUserByOpenId(storeId, sns, bindingId, cb) {
// 	cb (null, {name: '小胖', mobile: '10086'});
	db.queryWithParam("select * from member_sns_bind where store_id=? and sns=? and binding_id=?", [storeId, sns, bindingId], function (err, bind) {
		if (err) return cb(err);
		if (bind.length == 0) return cb();
		db.queryWithParam("select * from member where id=?", [bind[0].member_id], function (err, data) {
			if (err) return cb(err);
			cb(null, data[0]);
		});
	});
}
function getMemberByAccount(storeId, account, cb) {
	db.queryWithParamSingle("select * from member where mgmtDomain=? and (mobile=? or email=? or memberNumber=?)", [storeId, account, account, account], cb);
}
function getMemberById(id, cb) {
	if (!id) return cb(null, null);
	db.queryWithParam("select * from member where id=?", [id], function (err, data) {
		if (err) return cb(err);
		cb(null, data[0]);
	});
}

function getBindList(storeId, memberId, cb) {
	db.queryWithParam("select *  from member_sns_bind where store_id=?  and member_id=?", [storeId, memberId], cb);
}

function bind(storeId, member, sns, bindingId, cb) {
	db.queryWithParam("delete from member_sns_bind where store_id=? and sns=? and member_id=?", [storeId, sns, member.id], function (err) {
		if (err) return cb(err);
		var id = uuid.createId();
		db.queryWithParam("insert into member_sns_bind (id, store_id, sns, binding_id, member_id) values (?, ?, ?, ?, ?)", [id, storeId, sns, bindingId, member.id], cb);
	});
}


function getLastOrder(member, cb) {
	db.queryWithParamSingle("select * from orders where memberId = ? and ordertype!=? ORDER BY closeTime DESC limit 1", [member.id, 'macct-deposit'], cb);
}

function getInventory(member, page, rpp, cb) {
	db.queryWithParam("SELECT  oi.inventoryId,oi.inventoryName,SUM(qty) AS totl,SUM(AMOUNT) AS amount FROM orderitem oi " +
	"INNER JOIN orders o ON oi.ORDERS_ID =o.ID WHERE  inventoryId IS NOT NULL  AND " +
	"o.memberId = ? AND o.storeid=? GROUP BY oi.inventoryId ORDER BY totl DESC, " +
	"oi.inventoryId LIMIT ?, ? ", [member.id, member.mgmtdomain, page * rpp, rpp], function (err, data) {
		if (err) return cb(err);
		if (data.length == 0) return cb(null, null);
		cb(null, data);
	});
}

